package com.daoImpl;

import java.sql.Timestamp;
import java.util.List;

import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.bean.MessageCenter;
import com.dao.MessageCenterDao;

/**
 * A data access object (DAO) providing persistence and search support for
 * MessageCenter entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.bean.MessageCenter
 * @author MyEclipse Persistence Tools
 */
public class MessageCenterDaoImpl extends HibernateDaoSupport implements MessageCenterDao {
	private static final Logger log = LoggerFactory
			.getLogger(MessageCenterDaoImpl.class);
	// property constants
	public static final String TYPEID = "typeid";
	public static final String FLAGS = "flags";
	public static final String TELNO = "telno";
	public static final String OPENID = "openid";
	public static final String EMAILS = "emails";
	public static final String MESSAGE = "message";
	public static final String ERROR_NUMS = "errorNums";
	public static final String NOTE = "note";

	protected void initDao() {
		// do nothing
	}

	/* (non-Javadoc)
	 * @see com.bean.MessageCenterDao#save(com.bean.MessageCenter)
	 */
	@Override
	public void save(MessageCenter transientInstance) {
		log.debug("saving MessageCenter instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.bean.MessageCenterDao#delete(com.bean.MessageCenter)
	 */
	@Override
	public void delete(MessageCenter persistentInstance) {
		log.debug("deleting MessageCenter instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.bean.MessageCenterDao#findById(java.lang.Integer)
	 */
	@Override
	public MessageCenter findById(java.lang.Integer id) {
		log.debug("getting MessageCenter instance with id: " + id);
		try {
			MessageCenter instance = (MessageCenter) getHibernateTemplate()
					.get("com.bean.MessageCenter", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.bean.MessageCenterDao#findByExample(com.bean.MessageCenter)
	 */
	@Override
	public List findByExample(MessageCenter instance) {
		log.debug("finding MessageCenter instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.bean.MessageCenterDao#findByProperty(java.lang.String, java.lang.Object)
	 */
	@Override
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding MessageCenter instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from MessageCenter as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.bean.MessageCenterDao#findByTypeid(java.lang.Object)
	 */
	@Override
	public List findByTypeid(Object typeid) {
		return findByProperty(TYPEID, typeid);
	}

	/* (non-Javadoc)
	 * @see com.bean.MessageCenterDao#findByFlags(java.lang.Object)
	 */
	@Override
	public List findByFlags(Object flags) {
		return findByProperty(FLAGS, flags);
	}

	/* (non-Javadoc)
	 * @see com.bean.MessageCenterDao#findByTelno(java.lang.Object)
	 */
	@Override
	public List findByTelno(Object telno) {
		return findByProperty(TELNO, telno);
	}

	/* (non-Javadoc)
	 * @see com.bean.MessageCenterDao#findByOpenid(java.lang.Object)
	 */
	@Override
	public List findByOpenid(Object openid) {
		return findByProperty(OPENID, openid);
	}

	/* (non-Javadoc)
	 * @see com.bean.MessageCenterDao#findByEmails(java.lang.Object)
	 */
	@Override
	public List findByEmails(Object emails) {
		return findByProperty(EMAILS, emails);
	}

	/* (non-Javadoc)
	 * @see com.bean.MessageCenterDao#findByMessage(java.lang.Object)
	 */
	@Override
	public List findByMessage(Object message) {
		return findByProperty(MESSAGE, message);
	}

	/* (non-Javadoc)
	 * @see com.bean.MessageCenterDao#findByErrorNums(java.lang.Object)
	 */
	@Override
	public List findByErrorNums(Object errorNums) {
		return findByProperty(ERROR_NUMS, errorNums);
	}

	/* (non-Javadoc)
	 * @see com.bean.MessageCenterDao#findByNote(java.lang.Object)
	 */
	@Override
	public List findByNote(Object note) {
		return findByProperty(NOTE, note);
	}

	/* (non-Javadoc)
	 * @see com.bean.MessageCenterDao#findAll()
	 */
	@Override
	public List findAll() {
		log.debug("finding all MessageCenter instances");
		try {
			String queryString = "from MessageCenter";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.bean.MessageCenterDao#merge(com.bean.MessageCenter)
	 */
	@Override
	public MessageCenter merge(MessageCenter detachedInstance) {
		log.debug("merging MessageCenter instance");
		try {
			MessageCenter result = (MessageCenter) getHibernateTemplate()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.bean.MessageCenterDao#attachDirty(com.bean.MessageCenter)
	 */
	@Override
	public void attachDirty(MessageCenter instance) {
		log.debug("attaching dirty MessageCenter instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.bean.MessageCenterDao#attachClean(com.bean.MessageCenter)
	 */
	@Override
	public void attachClean(MessageCenter instance) {
		log.debug("attaching clean MessageCenter instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static MessageCenterDao getFromApplicationContext(
			ApplicationContext ctx) {
		return (MessageCenterDao) ctx.getBean("MessageCenterDao");
	}
}